<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@include file="/context/mytags.jsp" %>
<div class="portlet box green">
    <div class="portlet-title">
        <div class="caption">
            <i class="fa fa-gift"></i>医院
        </div>
    </div>
    <div class="portlet-body form">
        <form id="hospitalDepartmentForm" class="form-horizontal form-bordered" action="/hospitals/saveHospitalDepartment" method="post">
            <div class="form-group">
                <label class="col-md-3 control-label">一级科室:</label>
                <div class="col-md-9">
                    <div class="checkbox-list">
                        <label class="checkbox-inline">
                            <input type="checkbox" id="weightOneId" onclick="selectAll(this)"> 全选
                        </label>
                        <br/><br/>
                        <c:forEach items="${listWeightOne}" var="department" varStatus="vs">
                            <c:if test="${(vs.index)%6 == 0}">
                                <br/>
                            </c:if>
                            <label class="checkbox-inline" >
                                <input type="checkbox" name="weightOneId"
                                       value="${department.departmentId}" onclick="checkChecked(this)">
                                    ${department.departmentName}
                            </label>
                        </c:forEach>
                        <input type="hidden" name="weightOneIds" id="weightOneIds"/>
                    </div>
                </div>
            </div>
            <div class="form-group ">
                <label class="col-md-3 control-label">二级科室:</label>
                <div class="col-md-9">
                    <div class="checkbox-list">
                        <label class="checkbox-inline">
                            <input type="checkbox" id="weightTwoId" onclick="selectAll(this)"> 全选
                        </label>
                        <br/><br/>
                        <c:forEach items="${listWeightTwo}" var="department" varStatus="vs">
                            <c:if test="${(vs.index)%6 == 0}">
                                <br/>
                            </c:if>
                            <label class="checkbox-inline">
                                <input type="checkbox" name="weightTwoId"
                                       value="${department.departmentId}" onclick="checkChecked(this)">
                                    ${department.departmentName}
                            </label>
                        </c:forEach>
                        <input type="hidden" name="weightTwoIds" id="weightTwoIds"/>
                    </div>
                </div>
            </div>

            <div class="form-actions">
                <div class="col-md-9 col-md-offset-3">
                    <button type="button" class="btn btn-primary" onclick="saveHospitalDepartment(this)">
                        <spring:message code="btn.save"></spring:message>
                    </button>
                    <button type="reset" class="btn btn-default" onclick="cancel()">
                        <spring:message code="btn.return"></spring:message>
                    </button>
                </div>
            </div>
            <input type="hidden" name="id" value="${hospitals.id}">
        </form>
    </div>
</div>
<script type="text/javascript" src="<c:url value="/static/custom/datepicker-util.js"></c:url>"></script>
<script type="text/javascript">
    $(function () {
        $('#hospitalDepartmentForm').validate();
    });

    /**
     * checkbox全选
     * @param checkboxName checkbox的name属性名
     */
    function selectAll(obj){
        if($(obj).attr("checked")){
            $("input[name='" + obj.id + "']").attr("checked",true);
        }else{
            $("input[name='" + obj.id + "']").attr("checked",false);
        }
        selectChecked(obj.id)
    }

    /**
     * checkbox选择后验证全选按钮是否需要选择
     * @param obj checkbox
     */
    function checkChecked(obj){
        var checkSuccess = 0;
        $("input[name='" + obj.name + "']").each(function(){
            if(!$(this).is(":checked")){
                $("input[id='" + obj.name + "']").attr("checked",false);
                checkSuccess = 1;
                return false;
            }
        });
        if (0 == checkSuccess){
            $("input[id='" + obj.name + "']").attr("checked",true);
        }
        selectChecked(obj.name);
    }

    /**
     * 将checkbox选择的值放入对应的input中
     * @param obj checkbox，其checkbox的name属性名加上“s”即为对应的input的id
     */
    function selectChecked(checkboxName){
        var inputId = checkboxName + "s";
        var checkboxIds = "";
        $("input[name='" + checkboxName + "']").each(function(){
            if($(this).is(":checked")){
                checkboxIds = checkboxIds + "," + this.value;
            }
        });
        if ("" != checkboxIds){
            checkboxIds = checkboxIds.substr(1)
        }
        $("#" + inputId).val(checkboxIds);
    }

    function saveHospitalDepartment(obj) {
        var weightOneIds = $("#weightOneIds").val();
        var weightTwoIds = $("#weightTwoIds").val();
        if(null == weightOneIds || "" == weightOneIds){
            alert("请选择一级科室");
            return;
        }
        if(null == weightTwoIds || "" == weightTwoIds){
            alert("请选择二级科室");
            return;
        }

        if ($('#hospitalDepartmentForm').valid()) {

            var formData = new FormData($('#hospitalDepartmentForm')[0]);
            $.ajax({
                type: 'post',
                url: '<c:url value="/hospitals/saveHospitalDepartment"></c:url>',
                data: formData,
                async: false,
                cache: false,
                contentType: false,
                processData: false,
                dataType: 'json',
                success: function (data) {
                    alert(messages[data.result]);
                    search();
                    $(".panel-search").show();
                }
            });
        }
    }
</script>